package com.jiuwan.gamepass.service.impl;

import com.jiuwan.gamepass.dao.TotalDao;
import com.jiuwan.gamepass.dto.appJson.TotalPackageJson;
import com.jiuwan.gamepass.dto.appJson.TotalUrlListJson;
import com.jiuwan.gamepass.pojo.Total;
import com.jiuwan.gamepass.service.SDKUploadService;
import com.jiuwan.gamepass.utils.MyCode;
import com.jiuwan.gamepass.utils.ImageSizeUtil;
import com.jiuwan.gamepass.utils.StringSplitUtil;
import com.jiuwan.gamepass.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-11 14:11
 */

@Service
@Slf4j
public class SDKUploadServiceImpl implements SDKUploadService {
    //icon图片限制
    private static final String SIZE_ICON = "1024*1024";


    //定义目标路径，就是我们图片上传到的位置
    private static final String BASE_PATH_SDK = "G:\\MyProgran\\SDK";
    //定义访问图片的路径
    private static final String SERVER_PATH_SDK = "http://10.41.0.37:8080/SDK/";

    private static final String BASE_PATH_SDKController = "G:\\MyProgran\\SDKController";
    private static final String SERVER_PATH_SDKController = "http://10.41.0.37:8080/SDKController/";


    //icon目标路径 以及访问地址
    private static final String BASE_PATH_ICON = "G:\\MyProgran\\SDKController";
    private static final String SERVER_PATH_ICON = "http://10.41.0.37:8080/SDKController/";

    @Autowired
    private TotalDao totalDao;


    @Override
    public TotalPackageJson uploadFile(MultipartFile[] files, String appID) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String imagePath = "";
        if (files == null || files.length == 0) {
            totalPackageJson.setCode(0);
            totalPackageJson.setMsg("请选中要上传的文件");

            return totalPackageJson;
        }


        Total total = totalDao.selectByAppID(appID);


        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> imageList = new ArrayList<>();

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];


//---------------开始上传文件--------------------

            //获得上传文件的名称
            String filename = file.getOriginalFilename();

            //使用工具类获取UUID
            String uuid = UUIDUtil.createUUID();

            //利用原名和UUID生成新的文件名称
            String newFilename = uuid + "-" + filename;

            //创建文件的实例对象
            File image = new File(BASE_PATH_SDK, newFilename);

            //目录不存在则进行创建
            if (!image.exists()) {
                image.mkdirs();
            }

            //对文件进行上传操作
            try {
                file.transferTo(image);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            //------------------上传完毕-------------------
            imagePath = imagePath + SERVER_PATH_SDK + newFilename + ",";

            map.put("path-" + i, SERVER_PATH_SDK + newFilename);
            imageList.add(image);

        }
        totalPackageJson.setCode(1);
        totalPackageJson.setMsg("上传成功");

        totalPackageJson.setData(map);
        totalPackageJson.setTotalPackages(imageList);
        total.setSdk(imagePath);
        totalDao.updateByPrimaryKeySelective(total);
        return totalPackageJson;
    }



    @Override
    public TotalPackageJson upLoadFile(MultipartFile sdkController, String appID) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String sdkControllerPath = "";
        Total total = totalDao.selectByAppID(appID);

        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> imageList = new ArrayList<>();

        //------------开始上传文件------
        //获取上传文件的名称
        String filename = sdkController.getOriginalFilename();

        //创建文件的实例对象
        File file = new File(BASE_PATH_SDKController, filename);

        //目录不存在则进行创建
        if (!file.exists()) {
            file.mkdirs();
        }

        //对文件进行上传操作

        try {
            sdkController.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //--------------
        sdkControllerPath = SERVER_PATH_SDKController + filename;

        total.setSdkController(sdkControllerPath);
        int i = totalDao.updateByPrimaryKeySelective(total);
        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        map.put("sdkController-Path:", sdkControllerPath);
        return totalPackageJson;

    }

    @Override
    public TotalPackageJson uploadIcon(MultipartFile[] icons, String appID) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        Total total = totalDao.selectByAppID(appID);
        String icon = "";

        if (icons == null || icons.length == 0) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG);
            return totalPackageJson;
        }

        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < icons.length; i++) {
            MultipartFile file = icons[i];
            //限制获取图片的大小
            String s = ImageSizeUtil.imageSize(file);
            System.out.println(s);
            if (s.equals(SIZE_ICON) == false) {
                totalPackageJson.setCode(MyCode.FAIL_CODE);
                totalPackageJson.setMsg(MyCode.FAIL_MSG + "尺寸问题，必须1024*1024");
                return totalPackageJson;
            }
//---------------开始上传文件--------------------

            //获得上传文件的名称
            String filename = file.getOriginalFilename();

            //使用工具类获取UUID
            String uuid = UUIDUtil.createUUID();

            //利用原名和UUID生成新的文件名称
            String newFilename = uuid + "-" + filename;

            //创建文件的实例对象
            File path = new File(BASE_PATH_ICON, newFilename);

            //目录不存在则进行创建
            if (!path.exists()) {
                path.mkdirs();
            }

            //对文件进行上传
            try {
                file.transferTo(path);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            //--------上传完毕-----------
            icon = icon + SERVER_PATH_ICON + newFilename + ",";
            map.put("path-" + i + ":", SERVER_PATH_SDK + newFilename);
            list.add(icon);
        }
        total.setIcon(icon);
        int i = totalDao.updateByPrimaryKeySelective(total);
        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        return totalPackageJson;
    }

    @Override
    public TotalPackageJson uploadBackGround(MultipartFile background, String appID, String size) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        Total total = totalDao.selectByAppID(appID);
        String backGroundPath = "";

        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        if (background == null) {
            totalPackageJson.setCode(0);
            totalPackageJson.setMsg("请选中要上传的文件");
            return totalPackageJson;
        }
        //限制获取图片的大小
        String s = ImageSizeUtil.imageSize(background);
        if (s.equals(size) == false) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG + "尺寸失败，必须是");
            return totalPackageJson;
        }


        //------------开始上传文件------
        //获取上传文件的名称
        String filename = background.getOriginalFilename();

        //创建文件的实例对象
        File file = new File(MyCode.BASE_PATH_BACKGROUND, filename);

        //目录不存在则进行创建
        if (!file.exists()) {
            file.mkdirs();
        }

        //对文件进行上传操作

        try {
            background.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //--------------
        backGroundPath = MyCode.SERVER_PATH_BACKGROUND + filename;
        map.put("path-", MyCode.SERVER_PATH_BACKGROUND + filename);

        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        totalPackageJson.setData(map);
        total.setBackGround(backGroundPath);
        totalDao.updateByPrimaryKeySelective(total);
        return totalPackageJson;
    }

    @Override
    public TotalPackageJson uploadMaterial(MultipartFile[] materials, String appID) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String materialsPath = "";
        if (materials == null || materials.length == 0) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG);
            return totalPackageJson;
        }

        Total total = totalDao.selectByAppID(appID);
        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> materialList = new ArrayList<>();

        //------------开始上传文件------------------
        for (int i = 0; i < materials.length; i++) {
            MultipartFile material = materials[i];
            //获得上传文件的名称
            String filename = material.getOriginalFilename();
            //使用工具类获取UUID
            String uuid = UUIDUtil.createUUID();

            //利用原名和UUID生成新的文件名称
            String newFilename = uuid + "-" + filename;

            //创建文件示例对象
            File filePath = new File(MyCode.BASE_PATH_Material, newFilename);

            //目录不存在则进行创建
            if (!filePath.exists()) {
                filePath.mkdirs();
            }

            //对文件进行上传操作
            try {
                material.transferTo(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //----------------上传完毕之后
            materialsPath = materialsPath + MyCode.SERVER_PATH_Material + newFilename + ",";
            map.put("path-" + i, MyCode.SERVER_PATH_Material + newFilename);
            materialList.add("path-" + i + " " + MyCode.SERVER_PATH_Material + newFilename);


        }
        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        totalPackageJson.setTotalPackages(materialList);
        totalPackageJson.setData(map);
        total.setMaterial(materialsPath);
        int i = totalDao.updateByPrimaryKeySelective(total);
        log.info("是否更新成功：{}", i);
        return totalPackageJson;
    }


    //--------------------------------上传文件工具-----------------------------------------
    @Override
    public TotalPackageJson uploadSingleFile(MultipartFile file) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String uploadSingleFilePath = "";

        ArrayList<Object> fileList = new ArrayList<>();

        //------------开始上传文件------
        //获取上传文件的名称
        String filename = file.getOriginalFilename();

        //创建文件的实例对象
        File filePath = new File(BASE_PATH_SDKController, filename);

        //目录不存在则进行创建
        if (!filePath.exists()) {
            filePath.mkdirs();
        }

        //对文件进行上传操作

        try {
            file.transferTo(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //--------------
        uploadSingleFilePath = SERVER_PATH_SDKController + filename;


        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);

        // fileList.add("Path : "+uploadSingleFilePath);
        fileList.add(uploadSingleFilePath);
        totalPackageJson.setTotalPackages(fileList);
        return totalPackageJson;
    }

    @Override
    public TotalPackageJson uploadManyFile(MultipartFile[] sdks) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String sdkPath = "";
        if (sdks == null || sdks.length == 0) {
            totalPackageJson.setCode(0);
            totalPackageJson.setMsg("请选中要上传的文件");

            return totalPackageJson;
        }


        ArrayList<Object> sdkList = new ArrayList<>();

        for (int i = 0; i < sdks.length; i++) {
            MultipartFile file = sdks[i];
//---------------开始上传文件--------------------

            //获得上传文件的名称
            String filename = file.getOriginalFilename();

            //使用工具类获取UUID
            String uuid = UUIDUtil.createUUID();

            //利用原名和UUID生成新的文件名称
            String newFilename = uuid + "-" + filename;

            //创建文件的实例对象
            File image = new File(BASE_PATH_SDK, newFilename);

            //目录不存在则进行创建
            if (!image.exists()) {
                image.mkdirs();
            }

            //对文件进行上传操作
            try {
                file.transferTo(image);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            //------------------上传完毕-------------------
            sdkPath = SERVER_PATH_SDK + newFilename;


            // sdkList.add("sdkPath-"+i+" : "+sdkPath);
            sdkList.add(sdkPath);
        }
        totalPackageJson.setCode(1);
        totalPackageJson.setMsg("上传成功");

        totalPackageJson.setTotalPackages(sdkList);

        return totalPackageJson;
    }

    @Override
    public TotalPackageJson uploadManyImage(MultipartFile[] images, String size) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String imagesPath = "";

        if (images == null || images.length == 0) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG + " ：上传个数为0");
            return totalPackageJson;
        }

        HashMap<String, String> map = new HashMap<>();
        ArrayList<Object> imagelist = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            MultipartFile file = images[i];
            //限制获取图片的大小
            String s = ImageSizeUtil.imageSize(file);
            System.out.println(s);
            if (size != null && s.equals(size) == false) {
                totalPackageJson.setCode(MyCode.FAIL_CODE);
                totalPackageJson.setMsg(MyCode.FAIL_MSG + "尺寸问题，必须为" + size);
                return totalPackageJson;
            }
//---------------开始上传文件--------------------

            //获得上传文件的名称
            String filename = file.getOriginalFilename();

            //使用工具类获取UUID
            String uuid = UUIDUtil.createUUID();

            //利用原名和UUID生成新的文件名称
            String newFilename = uuid + "-" + filename;

            //创建文件的实例对象
            File path = new File(BASE_PATH_ICON, newFilename);

            //目录不存在则进行创建
            if (!path.exists()) {
                path.mkdirs();
            }

            //对文件进行上传
            try {
                file.transferTo(path);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            //--------上传完毕-----------
            imagesPath = SERVER_PATH_ICON + newFilename;
            //  imagelist.add("imagePath-"+i+" "+imagesPath);
            imagelist.add(imagesPath);
        }

        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        totalPackageJson.setTotalPackages(imagelist);
        return totalPackageJson;
    }

    @Override
    public TotalPackageJson uploadSingleImage(MultipartFile image, String size) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        String imagesPath = "";

        if (image == null) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG + " ：上传个数为0");
            return totalPackageJson;
        }


        ArrayList<Object> imagelist = new ArrayList<>();

        //限制获取图片的大小
        String s = ImageSizeUtil.imageSize(image);
        System.out.println(s);
        if (size != null & s.equals(size) == false) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG + "尺寸问题，必须为" + size);
            return totalPackageJson;
        }
//---------------开始上传文件--------------------

        //获得上传文件的名称
        String filename = image.getOriginalFilename();

        //使用工具类获取UUID
        String uuid = UUIDUtil.createUUID();

        //利用原名和UUID生成新的文件名称
        String newFilename = uuid + "-" + filename;

        //创建文件的实例对象
        File path = new File(BASE_PATH_ICON, newFilename);

        //目录不存在则进行创建
        if (!path.exists()) {
            path.mkdirs();
        }

        //对文件进行上传
        try {
            image.transferTo(path);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        //--------上传完毕-----------
        imagesPath = SERVER_PATH_ICON + newFilename;
        // imagelist.add("imagePath : "+" "+imagesPath);
        imagelist.add(imagesPath);


        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        totalPackageJson.setTotalPackages(imagelist);
        return totalPackageJson;
    }


    //---------------------------------将地址放入total------------------

    @Override
    public TotalPackageJson setAddressInTotal(String appID, String sdk, String sdkController, String icons, String backGround, String materials) {
        TotalPackageJson totalPackageJson = new TotalPackageJson();
        ArrayList<Object> list = new ArrayList<>();
        Total totalByAppID = totalDao.selectByAppID(appID);

        if (totalByAppID==null){
            Total total = new Total();
            String uuid = UUIDUtil.createUUID();
            total.setId(uuid);
            total.setAppID(appID);
            Date date = new Date();
            total.setCreateTime(date);
            total.setSdk(sdk);
            total.setIcon(icons);
            total.setSdkController(sdkController);
            total.setMaterial(materials);
            total.setBackGround(backGround);
            int i = totalDao.insertSelective(total);
            if (i == 0) {
                totalPackageJson.setCode(MyCode.FAIL_CODE);
                totalPackageJson.setMsg(MyCode.FAIL_MSG);
                return totalPackageJson;
            }
            totalPackageJson.setCode(MyCode.SUCCESS_CODE);
            totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
            list.add(total);
            totalPackageJson.setTotalPackages(list);
            return totalPackageJson;
        }




        totalByAppID.setSdk(sdk);
        totalByAppID.setSdkController(sdkController);
        totalByAppID.setIcon(icons);
        totalByAppID.setBackGround(backGround);
        totalByAppID.setMaterial(materials);

        int i = totalDao.updateByPrimaryKeySelective(totalByAppID);

        if (i == 0) {
            totalPackageJson.setCode(MyCode.FAIL_CODE);
            totalPackageJson.setMsg(MyCode.FAIL_MSG);
            return totalPackageJson;
        }
        Total total1 = totalDao.selectByAppID(appID);
        list.add(total1);
        totalPackageJson.setCode(MyCode.SUCCESS_CODE);
        totalPackageJson.setMsg(MyCode.SUCCESS_MSG);
        totalPackageJson.setTotalPackages(list);
        return totalPackageJson;
    }

    @Override
    public TotalUrlListJson urlPath(String appId) {
        TotalUrlListJson totalUrlListJson = new TotalUrlListJson();
        if (appId==null){
                totalUrlListJson.setCode(MyCode.FAIL_CODE);
                totalUrlListJson.setMsg(MyCode.FAIL_MSG+"-您的参数未输入");
                return totalUrlListJson;
        }
        Total total = totalDao.selectByAppID(appId);
        if (total==null){
            totalUrlListJson.setCode(MyCode.FAIL_CODE);
            totalUrlListJson.setMsg(MyCode.FAIL_MSG+"-您查询的sdk还未上传相关文件");
            return totalUrlListJson;
        }



        totalUrlListJson.setSdk(StringSplitUtil.urlSplit(total.getSdk()));
        totalUrlListJson.setSdkController(StringSplitUtil.urlSplit(total.getSdkController()));
        totalUrlListJson.setIcon(StringSplitUtil.urlSplit(total.getIcon()));
        totalUrlListJson.setBackGround(StringSplitUtil.urlSplit(total.getBackGround()));
        totalUrlListJson.setMaterial(StringSplitUtil.urlSplit(total.getMaterial()));
        totalUrlListJson.setTotal(total);
        totalUrlListJson.setCode(MyCode.SUCCESS_CODE);
        totalUrlListJson.setMsg(MyCode.SUCCESS_MSG);

        return totalUrlListJson;

    }
}